package tasks;

import jade.core.Agent;
import jade.core.behaviours.DataStore;

import jade.lang.acl.ACLMessage;

import jade.lang.acl.UnreadableException;

import jade.proto.ContractNetInitiator;

import java.util.ArrayList;

public class ContractNetIB extends ContractNetInitiator {
    public ContractNetIB(Agent agent, ACLMessage aclMessage,
                         DataStore dataStore) {
        super(agent, aclMessage, dataStore);
    }

    public ContractNetIB(Agent agent, ACLMessage msg) {
        super(agent, msg);
    }
    
    protected void handleAllResponses(java.util.Vector responses,
                                      java.util.Vector acceptances) {
        //evaluate all proposals
        int idx = 0;
        for(Object obj : responses) {
            ACLMessage response = (ACLMessage)obj;
            try {
                ((ManagerAgent)myAgent).evaluateProposals(
                    (ArrayList<ArrayList<Long>>)response.getContentObject(),
                    idx);
                idx++;
            } catch (UnreadableException e) {
                e.printStackTrace();
                idx++;
                continue;
            }
            ACLMessage acc = response.createReply();
            acc.setPerformative(ACLMessage.REJECT_PROPOSAL);
            acc.setContent("");
            acceptances.add(acc);
        }
        ((ManagerAgent)myAgent).createAcceptances(acceptances);
    }
}
